//
//  HLDownLoadingCell.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/16.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//


#import "HLDownLoadingCell.h"
#import <Masonry/Masonry.h>
#import "HLCourseComment.h"
#import "HLCourseDetail.h"

@interface HLDownLoadingCell()

/** authorL  */
@property (nonatomic ,strong) UILabel *title;

/** timeL  */
@property (nonatomic ,strong) UIButton *pauseB;
/** timeL  */
@property (nonatomic ,strong) UIButton *deleteB;

/** 是否暂停  */
@property (nonatomic ,assign)BOOL isPauseState;

@end

@implementation HLDownLoadingCell


- (UILabel *)title
{
    if (!_title ) {
        _title = [[UILabel alloc] init];
        _title.text = @"航天航空知识简介";
        _title.textColor = HLRGB(60, 60, 60);
        _title.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 14:(kMainTitleFontSize + 2)];
    }
    return _title;
}


- (UILabel *)contentNum
{
    if (!_contentNum ) {
        _contentNum = [[UILabel alloc] init];
        _contentNum.text = @"  ";
        _contentNum.textAlignment = NSTextAlignmentRight;
        _contentNum.textColor = HLRGB(120, 120, 120);
        _contentNum.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 12:kMainTitleFontSize];
    }
    return _contentNum;
}

- (UIProgressView *)progressView
{
    if (!_progressView ) {
        _progressView = [UIProgressView new];
        _progressView.progress = 0;
    }
    return  _progressView;
}

- (UIButton *)pauseB
{
    if (!_pauseB ) {
        _pauseB = [[UIButton alloc] init];
        _pauseB.titleLabel.font = [UIFont systemFontOfSize:14];
        _pauseB.backgroundColor = HLRGB(220, 220, 220);
        _pauseB.layer.cornerRadius = 3;
        _pauseB.clipsToBounds = YES;
        [_pauseB setTitle:@"暂停" forState:UIControlStateNormal];
        [_pauseB setTitleColor:HLRGB(60, 60, 60) forState:UIControlStateNormal];
        [_pauseB addTarget:self action:@selector(pauseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pauseB;
}

- (UIButton *)deleteB
{
    if (!_deleteB ) {
        _deleteB = [[UIButton alloc]init];
        [_deleteB setImage:[UIImage imageNamed:@"mine_course_delete"] forState:UIControlStateNormal];
        [_deleteB addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteB;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
        
        self.isPauseState = NO;
    }
    return self;
}

- (void)setUI
{
    [self.contentView addSubview:self.title];
    [self.contentView addSubview:self.deleteB];
    [self.contentView addSubview:self.progressView];
    [self.contentView addSubview:self.contentNum];
    [self.contentView addSubview:self.pauseB];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self configsCons];
}

- (void)configsCons
{
    [self.pauseB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(40));
        make.height.equalTo(@(25));
    }];
    
    [self.deleteB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.pauseB.mas_left).offset(-5);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(30));
        make.height.equalTo(@(30));
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView).offset((self.height * 0.5 - 30 )*0.5);
        make.right.equalTo(self.deleteB.mas_left).offset(-5);
        make.height.equalTo(@(30));
    }];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deleteB.mas_left).offset(-5);
        make.centerY.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(15);
        make.height.equalTo(@(2));
    }];
    
    [self.contentNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(self.width *0.5));
        make.top.equalTo(self.progressView.mas_bottom).offset(5);
        make.right.equalTo(self.progressView);
        make.height.equalTo(@(20));
    }];
}

- (void)deleteButtonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(downLoadingCell:didClickDeleteCourseDetail:)]) {
        [self.delegate downLoadingCell:self didClickDeleteCourseDetail:self.courseDetail];
    }
}

- (void)pauseButtonClick:(UIButton *)button
{
    self.isPauseState = !self.isPauseState;
    self.courseDetail.isPauseState = self.isPauseState;
    self.courseDetail.currentProgress = self.progressView.progress;
    [button setTitle:self.isPauseState ? @"继续":@"暂停" forState:UIControlStateNormal];
    [button setTitleColor:self.isPauseState ? [UIColor whiteColor]: HLRGB(60 , 60, 60) forState:UIControlStateNormal];
    button.backgroundColor = self.isPauseState ? kColorMineBlueBG: HLRGB(220, 220, 220);
    
    if ([self.delegate respondsToSelector:@selector(downLoadingCell:didClickPauseCourseDetail:)]) {
        [self.delegate downLoadingCell:self didClickPauseCourseDetail:self.courseDetail];
    }
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.y += 1;
    frame.size.height = frame.size.height - 1;
    [super setFrame:frame];
}

- (void)setCourseDetail:(HLCourseDetail *)courseDetail
{
    _courseDetail = courseDetail;
    self.title.text = courseDetail.name;
    self.isPauseState = courseDetail.isPauseState;
    
    [self.pauseB setTitle:self.isPauseState ? @"继续":@"暂停" forState:UIControlStateNormal];
    [self.pauseB setTitleColor:self.isPauseState ? [UIColor whiteColor]: HLRGB(60 , 60, 60) forState:UIControlStateNormal];
    self.pauseB.backgroundColor = self.isPauseState ? kColorMineBlueBG: HLRGB(220, 220, 220);
    self.progressView.progress = courseDetail.currentProgress;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end
